const mongoose = require('mongoose')

mongoose.connect('mongodb://127.0.0.1:27017/bilibili')

mongoose.connection.once('open', () => {
  let BookSchema = new mongoose.Schema({
    name: String,
    author: String,
    price: Number,
    is_hot: Boolean,
    tags: Array,
    pub_time: Date,
    test: mongoose.Schema.Types.Mixed
  })

  let BookModel = mongoose.model('books', BookSchema)

  BookModel.create({
    name: '西游记',
    author: '吴承恩',
    price: 19.9,
    is_hot: true,
    tags: ['鬼怪', '励志', '玄幻'],
    pub_time: new Date(),
    test: new Date()
  })
    .then((data) => {
      console.log(data)
      mongoose.disconnect()
    })
    .catch((err) => {
      console.log(err)
    })

  // BookModel.create(
  //   {
  //     name: '西游记',
  //     author: '吴承恩',
  //     price: 19.9
  //   },
  //   (err, data) => {
  //     if (err) {
  //       console.log(err)
  //       return
  //     }
  //     console.log(data)
  //   }
  // )
})
mongoose.connection.on('error', () => {
  console.log('连接失败')
})
mongoose.connection.on('close', () => {
  console.log('连接关闭')
})
